home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: What should be returned?
- Date: Wed, 17 Jan 96 22:33:53 GMT
- Organization: none
- Message-ID: <821918033snz@genesis.demon.co.uk>
- References: <4dj8pv$cjd@eng_ser1.erg.cuhk.hk> <Crawford.821899744@mariner>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Crawford.821899744@mariner> crawford@iac.net "CRAWFORD" writes:
-
- >phsung@cs.cuhk.hk (the CAReLess boy) writes:
- >>Hi, all,
- >> It's said that the function main must return an integer value
- >>but I just don't know what value should be returned.
-
- The C language defines 3 values which may be passed portably to exit()
- and hence returned from an initial invocation of main:
-
- 0 - indicates a successful return
-
- EXIT_SUCCESS - indicates a successful return
-
- EXIT_FAILURE - indicates an unsuccessful return
-
- EXIT_SUCCESS and EXIT_FAILURE are defined (along with exit() in stdlib.h.
-
- While 0 and EXIT_SUCCESS mean the same thing, EXIT_SUCCESS doesn't
- have to be defined as 0 - there could be more than one value indicating
- success. Your implementation can define (or allow you to use) other values
- but these values can be used across all ANSI conforming implementations.
-
- >> Also, if I quit
- >>main by exit(), what's the use of the return value?
- >> Any help?
-
- No use at all. If code can never reach the end of a function in C there
- is no requirement for the function to end in a return statement. However a
- C function must always be defined with an equivalent return type to that
- which the caller expects (for example so that the correct call sequence for
- that function is generated). A C runtime system is entitled to expect to
- call a main function that returns int so, to be portable, you must provide
- it with one. Not returning from a function gives you no license to change the
- return type of the function, unless you do so in both the caller and callee.
- At program startup you have no control over the caller.
-
- > exit() also causes a value to be returned from main, the POSIX
- >prottype for exit() is:
- >
- >void exit(int status);
-
- POSIX simply adopted the ANSI C prototype for exit().
-
- > In the UNIX world (and, I believe, DOS), the standard is to
- >return zero on success and non-zero on a failure. That's what the
- >macros evaluate to, but I'd use the macros anyway, since they're easy
- >to understand.
-
- Zero is guaranteed to be transformed by the implementation in whatever way
- necessary to generate a successful termination condition. There are
- no guarantees for non-zero returns e.g. in VMS 1 is a success indication.
- EXIT_FAILURE is the only portable way of generating a failure indication.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-